package com.hnao.serialport.utils.sqym;


import android.util.Log;
import android_serialport_api.SerialPort;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidParameterException;

public class SerialPortKit {
    private static final String TAG = "SerialPortKit";
    private SerialPort mSerialPort;
    private OutputStream mOutputStream;
    private InputStream mInputStream;
    private ReadThread mReadThread;
    private Thread mWriteDataThread;
    private SerialPortListener mSerialPortListener;
    private boolean isSpecialSerialPort = false;
    private int dataStartBit = 1;
    private int dataLength = 10;

    public SerialPortKit() {
    }

    public void writeData(byte[] data) {
        this.mWriteDataThread = new WriteDataThread(data);
        this.mWriteDataThread.start();
    }

    private SerialPort getSerialPort(String path, int baudRate) throws SecurityException, IOException, InvalidParameterException {
        if (this.mSerialPort == null) {
            this.mSerialPort = new SerialPort(new File(path), baudRate, 0);
        }

        return this.mSerialPort;
    }

    public boolean openSerialPort(String path, int baudRate) {
        try {
            this.mSerialPort = this.getSerialPort(path, baudRate);
            this.mOutputStream = this.mSerialPort.getOutputStream();
            this.mInputStream = this.mSerialPort.getInputStream();
            this.mReadThread = new ReadThread();
            this.mReadThread.start();
            return true;
        } catch (SecurityException var4) {
            Log.e("SerialPortTest", "You do not have read/write permission to the serial port.");
            return false;
        } catch (IOException var5) {
            Log.e("SerialPortTest", "The serial port can not be opened for an unknown reason.");
            return false;
        } catch (InvalidParameterException var6) {
            Log.e("SerialPortTest", "Please configure your serial port first.");
            return false;
        } catch (Error var7) {
            Error var6 = var7;
            Log.e("SerialPortTest", var6.getMessage());
            return false;
        }
    }

    public void closeSerialPort() {
        if (this.mReadThread != null) {
            this.mReadThread.interrupt();
        }

        if (this.mWriteDataThread != null) {
            this.mWriteDataThread.interrupt();
        }

        if (this.mSerialPort != null) {
            this.mSerialPort.close();
            this.mSerialPort = null;
        }

    }

    public void setSpecialSerialPort() {
        this.isSpecialSerialPort = true;
    }

    public void setOnSerialPortChanged(SerialPortListener spl, int dataStartBit, int dataLength) {
        this.mSerialPortListener = spl;
        this.dataLength = dataLength;
        this.dataStartBit = dataStartBit;
    }

    public static byte[] hexStringToBytes(String inputStr) {
        byte[] result = new byte[inputStr.length() / 2];

        for(int i = 0; i < inputStr.length() / 2; ++i) {
            result[i] = (byte)(Integer.parseInt(inputStr.substring(i * 2, i * 2 + 2), 16) & 255);
        }

        return result;
    }

    public interface SerialPortListener {
        void onSerialPortChanged(byte[] var1);
    }

    private class ReadThread extends Thread {
        private ReadThread() {
        }

        public void run() {
            while(!this.isInterrupted()) {
                try {
                    byte[] buffer = new byte[64];
                    if (SerialPortKit.this.mInputStream != null) {
                        int size = SerialPortKit.this.mInputStream.read(buffer);
                        if (SerialPortKit.this.isSpecialSerialPort && size >= SerialPortKit.this.dataLength) {
                            byte[] cardBuffer = new byte[SerialPortKit.this.dataLength];
                            System.arraycopy(buffer, SerialPortKit.this.dataStartBit, cardBuffer, 0, SerialPortKit.this.dataLength);
                            if (SerialPortKit.this.mSerialPortListener != null) {
                                SerialPortKit.this.mSerialPortListener.onSerialPortChanged(cardBuffer);
                            }
                        }
                    }
                } catch (IOException var4) {
                    IOException var3 = var4;
                    var3.printStackTrace();
                    return;
                }
            }

        }
    }

    private class WriteDataThread extends Thread {
        private byte[] mData;

        WriteDataThread(byte[] data) {
            this.mData = data;
        }

        public void run() {
            if (SerialPortKit.this.mOutputStream != null) {
                try {
                    SerialPortKit.this.mOutputStream.write(this.mData);
                } catch (IOException var2x) {
                    IOException var2 = var2x;
                    var2.printStackTrace();
                }
            }

        }
    }
}
